package com.broadengate.action;

import java.util.Date;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.broadengate.form.DepositForm;
import com.broadengate.model.Atminfo;
import com.broadengate.model.Card;
import com.broadengate.model.Deposit;
import com.broadengate.service.AdminService;
import com.broadengate.service.CardService;
import com.broadengate.service.CurrencyService;
import com.broadengate.service.DepositService;

public class DepositAction extends DispatchAction{

	private DepositService depositService;
	private CardService cardService;
	private CurrencyService currencyService;
	
	/******
	 * 实现用户存款
	 * 核心功能由调用AdminService对象的方法depositList()，withdrawList()，trasferList()实现
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward 对象
	 */
	public ActionForward deposit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {			
			
			System.out.println("DepositAction 进入");
			   HttpSession session=request.getSession();
			DepositForm depositForm = (DepositForm) form;
			float ammout = Float.parseFloat(depositForm.getSavemoney());
			int i=(int)ammout;
			String cardNO=request.getSession().getAttribute("cardNO").toString();	
			//String cardNO=request.getParameter("cardNO");
			Card cardX=cardService.findCardByCardNO(cardNO);
			
			String currencyName="rmb";	
			if((Math.abs(ammout-i)>=0.00001f )|| (i%100!=0)){
				session.setAttribute("wrong","notzd");
				return mapping.findForward("fail");
			}
			else{
			if(depositForm.getSave().equals("rmb")){
				currencyName="人民币";
				cardX.setRmbbalance(cardX.getRmbbalance()+ammout);
				System.out.println(cardX.getRmbbalance()+ammout);
			}
			else if(depositForm.getSave().equals("dollar")){
				currencyName="美元";
				cardX.setDollarBalance(cardX.getDollarBalance()+ammout);
			}
			else if(depositForm.getSave().equals("euro")){
				currencyName="欧元";
				cardX.setEuroBalance(cardX.getEuroBalance()+ammout);
			}
			}

							
			Deposit deposit=new Deposit();
			deposit.setAmount(ammout);
			deposit.setCard(cardX);
			deposit.setCurrency(currencyService.findCurrencyByName(currencyName));
			deposit.setTime(new Date());
			
			try {
				depositService.deposit(cardX, deposit);
				return mapping.findForward("success");
			} catch (Exception e) {
				e.printStackTrace();
				return mapping.findForward("fail");
			}
	}

	public DepositService getDepositService() {
		return depositService;
	}

	public void setDepositService(DepositService depositService) {
		this.depositService = depositService;
	}

	public CardService getCardService() {
		return cardService;
	}

	public void setCardService(CardService cardService) {
		this.cardService = cardService;
	}

	public CurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setCurrencyService(CurrencyService currencyService) {
		this.currencyService = currencyService;
	}


}


